#import "OmHomeVC.h"
#import "OmUserPageVC.h"
#import "OrderIndexViewController.h"
#import "OmHomeVCCollectionCell.h"
#import "PageDataApiRequestGetIndexOnlineModel.h"
#import "MJExtension.h"
#import "HomeListSearchViewController.h"
@interface OmHomeVC () <UICollectionViewDelegate, UICollectionViewDataSource, UIScrollViewRefreshDelegate>
@property (nonatomic, strong) UICollectionView *beautyList;
@end
@implementation OmHomeVC
#pragma mark - ConstructionScrip
- (void)configSelf {
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:ASLocalizedString(@"排行榜") style:UIBarButtonItemStylePlain target:self action:@selector(rankButtonTap)];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:ASLocalizedString(@"搜索") style:UIBarButtonItemStylePlain target:self action:@selector(searchButtonTap)];
}
- (void)allocateComponents {
    self.beautyList = (UICollectionView *)[[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:[self getCollectionViewLayout]]
    .fp_dataSource(self)
    .fp_delegate(self)
    .fp_enableRefreshAndSetRefreshDelegate(self)
    .fp_refreshPageCount(10)
    .fp_backgroundColor([UIColor qmui_colorWithHexString:@"#F7F7F7"]);
    [self.beautyList registerNib:[UINib nibWithNibName:kOmHomeVCCollectionCell bundle:nil] forCellWithReuseIdentifier:kOmHomeVCCollectionCell];
    [self.view addSubview:self.beautyList];
}
- (void)layoutComponents {
    [self.beautyList mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.equalTo(self.view);
    }];
}
- (void)refreshSelf {
    [self.beautyList.mj_header beginRefreshing];
}
#pragma mark - EventGraph
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.beautyList.refreshDataArray.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    OmHomeVCCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kOmHomeVCCollectionCell forIndexPath:indexPath];
    [cell setupContent:self.beautyList.refreshDataArray[indexPath.item]];
    return cell;
}
- (void)scrollView:(UIScrollView *)scrollView didBeginRefresh:(UIScrollView *(^)(NSMutableArray *))refreshData {
    NSString *url = [[CYURLUtils sharedCYURLUtils] makeURLWithC:@"page_data_api" A:@"request_get_index_online"];
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    param[@"page"] = @(self.beautyList.refreshPage);
    [CYNET POSTv3:url parameters:param responseCache:^(id responseObject) {
    } success:^(id responseObject) {
        NSArray *data = responseObject[@"data"];
        NSMutableArray *modelList = [PageDataApiRequestGetIndexOnlineModel mj_objectArrayWithKeyValuesArray:data];
        refreshData(modelList);
    } failure:^(NSString *error, NSInteger code) {
    } hasCache:YES];
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    OmUserPageVC *vc = [[OmUserPageVC alloc] init];
    PageDataApiRequestGetIndexOnlineModel *model = self.beautyList.refreshDataArray[indexPath.item];
    vc.ID = model.ID;
    [self.navigationController pushViewController:vc animated:YES];
}
- (void)rankButtonTap {
    OrderIndexViewController *order = [OrderIndexViewController new];
    [self.navigationController pushViewController:order animated:YES];
}
- (void)searchButtonTap {
    HomeListSearchViewController *search = [[HomeListSearchViewController alloc] init];
    [self.navigationController pushViewController:search animated:YES];
}
#pragma mark - Method
- (UICollectionViewFlowLayout *)getCollectionViewLayout {
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    CGFloat marginWidth = RPX(12);
    CGFloat itemWidth = (SCREEN_WIDTH - RPX(4) - marginWidth * 2) / 2.0;;
    CGFloat itemHeight = 105 + itemWidth - 24 + 16;
    layout.itemSize =CGSizeMake(itemWidth, itemHeight);
    layout.minimumInteritemSpacing = RPX(4);
    layout.minimumLineSpacing = RPX(20);
    layout.sectionInset = UIEdgeInsetsMake(marginWidth, marginWidth, marginWidth,marginWidth);
    return layout;
}
@end
